<?php
class Admin_CategoryController extends Zend_Controller_Action
{
	private $model;
	private $auth;
	private $acl;
	public function init(){
		$this->model = new Admin_Model_Category();
		$this->auth = Zend_Auth::getInstance();
		$this->acl = new Admin_Model_Access();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/admin";
		$dirlayout = APPLICATION_PATH . "/layouts/admin";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/style.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-ui-1.8.18.custom.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/nicEdit.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slidedown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/editor2.js');
		$this->view->headScript()->appendFile($dirfile . '/js/updateselect.js');
		$this->view->headScript()->appendFile($dirfile . '/js/addcategory.js');
		$this->view->headScript()->appendFile($dirfile . '/js/newcategory.js');
		$this->view->headScript()->appendFile($dirfile . '/js/sccategory.js');
		$this->view->headScript()->appendFile($dirfile . '/js/scecate.js');
		$this->view->headScript()->appendFile($dirfile . '/js/datepick.js');
		$this->view->headScript()->appendFile($dirfile . '/js/search.js');
		$this->view->headScript()->appendFile($dirfile . '/js/checkboxcategr.js');
		$this->view->headScript()->appendFile($dirfile . '/js/delcate.js');
		$this->view->headScript()->appendFile($dirfile . '/js/editcate.js');
		$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		Zend_Layout::startMvc($option);
	}
	public function indexAction()
	{
		if (!$this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','index'))
		{
			$this->_redirect('/error/denied');
		}
	}
	public function addAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','add'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		$name = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$id_user = $this->auth->getIdentity()->id_user;
		$textarea = $this->_request->getParam('textarea');
		$dirpath = APPLICATION_PATH . '/../public/';
		$name_folder = 'category'. $id;
		$dir_folder = $dirpath.$name_folder; 
		if(!is_dir($dir_folder))
		{
			mkdir($dir_folder,0775,true);
		}
		 try {
		 	
		$add = $this->model->add($id,$name, $date,$id_user, $textarea,$name_folder);
		echo "Add thanh cong";
		}catch(Zend_Db_Exception $e)
		{
			echo('Loi xu ly');
		} 
		//echo Zend_Debug::dump($this->auth->getIdentity());
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function manageAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','manage'))
		{
		$data = $this->model->getAll();
		$paging = Zend_Paginator::factory($data);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->paging = $paging;
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function vieweditAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','viewedit'))
		{
		$id = $this->_request->getParam('id');
		$data = $this->model->getId($id);
		$this->view->data = $data;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
		
	}
	public function editAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','edit'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		$name = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$id_user = $this->auth->getIdentity()->id_user;
		$textarea = $this->_request->getParam('textarea');
		 try{
			$update = $this->model->edit($id, $name, $date, $id_user, $textarea);
			echo "Edit thanh cong";
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		} 
		//echo $id_user;
		}
		else 
		{
			$this->_redirect('/error/denied');
		}
	}
	public function getsearchAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','getsearch'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$contents = $this->_request->getParam('text-search');
		$session = new Zend_Session_Namespace('search');
		$session->content = $contents;
		$this->_forward('search');
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function searchAction()
	{
		//$contents = $this->_request->getParam('text-search');	
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','search'))
		{
		$session = new Zend_Session_Namespace('search');
		$contents = $session->content;
		$search = $this->model->search($contents);
		$pagingsearch = Zend_Paginator::factory($search);
		$pagingsearch->setItemCountPerPage(2);
		$pagingsearch->setPageRange(3);
		$currentsearch = $this->_request->getParam('page',1);
		$pagingsearch->setCurrentPageNumber($currentsearch);
		$this->view->pagingsearch = $pagingsearch;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
	public function deleteAction()
	{
		if ($this->acl->isAllowed($this->auth->getIdentity()->id_group,'category','delete'))
		{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		try {
			$delete = $this->model->delete($id);
		}catch(Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		} 
		//echo $id;
		}
		else
		{
			$this->_redirect('/error/denied');
		}
	}
}